package com.gel.shortLink.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gel.shortLink.admin.common.biz.UserContext;
import com.gel.shortLink.admin.convention.exception.ServiceException;
import com.gel.shortLink.admin.convention.result.Result;
import com.gel.shortLink.admin.dao.entity.GroupDO;
import com.gel.shortLink.admin.dao.mapper.GroupDOMapper;
import com.gel.shortLink.admin.remote.dto.req.RecyclePageReqDTO;
import com.gel.shortLink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import com.gel.shortLink.admin.remote.service.ShortLinkRemoteService;
import com.gel.shortLink.admin.service.GroupService;
import com.gel.shortLink.admin.service.RecycleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class RecycleServiceImpl implements RecycleService {
    private final GroupDOMapper groupDOMapper;
    private final ShortLinkRemoteService shortLinkRemoteService=new ShortLinkRemoteService(){};
    @Override
    public Result<IPage<ShortLinkPageRespDTO>> pageRecycleShortLink() {
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getDelFlag, 0);
        List<GroupDO> groupList=groupDOMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(groupList)){
            throw  new ServiceException("用户无分组信息");
        }
        RecyclePageReqDTO requestParam=new RecyclePageReqDTO();
        requestParam.setGidList(groupList.stream().map(GroupDO::getGid).toList());
        return shortLinkRemoteService.pageRecycleShortLink(requestParam);


    }
}
